<template>
  <view>
    <scroll-view scroll-y style="height: 100%; width: 100%">
      <view class="swiper-div u-flex-col">
        <u-card :border="false" box-shadow="0px 0px 8px #ddd6d6" :body-style="{ padding: '0' }" :show-head="false">
          <view slot="body">
            <view class="card-row">
              <u-cell-group :border="false">
                <u-cell-item
                  v-for="(item, index) in safeListOne"
                  title-width="310"
                  :key="'safeListOne_' + index"
                  :title-style="{ color: '#333333' }"
                  :value-style="{
                    display: 'flex',
                    paddingLeft: '20rpx',
                    color: '#666666',
                  }"
                  :arrow="false"
                  :value="list[item.value]"
                >
                  <text slot="title" v-html="item.name"></text>
                </u-cell-item>
              </u-cell-group>
            </view>
          </view>
        </u-card>
        <u-card :border="false" box-shadow="0px 0px 8px #ddd6d6" :body-style="{ padding: '0' }" :show-head="false">
          <view slot="body">
            <view class="card-row">
              <u-cell-group :border="false">
                <u-cell-item
                  v-for="(item, index) in safeListTwo"
                  :key="'safeListTwo_' + index"
                  title-width="310"
                  :title-style="{ color: '#333333' }"
                  :value-style="{
                    display: 'flex',
                    paddingLeft: '20rpx',
                    color: '#666666',
                  }"
                  :arrow="false"
                  :value="getValue(item.value)"
                >
                  <text slot="title" v-html="item.name"></text>
                </u-cell-item>
                <!-- #ifndef  H5 -->
                <u-cell-item
                  title="隐患排查治理制度"
                  :title-style="{ color: '#333333' }"
                  :value-style="{
                    display: 'flex',
                    paddingLeft: '20rpx',
                    color: '#3070FF',
                  }"
                  :arrow="false"
                ></u-cell-item>
                <u-cell-item
                  :border-bottom="false"
                  v-for="(item, index) in fileOne"
                  :key="'fileOne_' + index"
                  :title="`${index + 1}、${item.archivesName} ${item.fileType ? '.' + item.fileType : ''}`"
                  :title-style="{ color: '#3070FF' }"
                  :arrow="false"
                  @click="goLearn(item)"
                ></u-cell-item>
                <u-cell-item
                  title="隐患排查治理计划"
                  :title-style="{ color: '#333333' }"
                  :value-style="{
                    display: 'flex',
                    paddingLeft: '20rpx',
                    color: '#3070FF',
                  }"
                  :arrow="false"
                ></u-cell-item>
                <u-cell-item
                  :border-bottom="false"
                  v-for="(item, index) in fileTwo"
                  :key="'fileTwo_' + index"
                  :title="`${index + 1}、${item.archivesName} ${item.fileType ? '.' + item.fileType : ''}`"
                  :title-style="{ color: '#3070FF' }"
                  :arrow="false"
                  @click="goLearn(item)"
                ></u-cell-item>
                <!-- #endif -->
              </u-cell-group>
            </view>
          </view>
        </u-card>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { BASE_IP, BASE_URL } from '@/utils/ip.js'
export default {
  name: 'index',
  props: {
    customerId: String
  },
  data() {
    return {
      BASE_IP: '',
      BASE_URL: '',
      safeListOne: [
        {
          name: '主要负责人',
          value: 'main'
        },
        {
          name: '主要负责人<br>固定电话号码',
          value: 'mainFix'
        },
        {
          name: '主要负责人<br>移动电话号码',
          value: 'mainMobile'
        },
        {
          name: '主要负责人<br>电子邮箱',
          value: 'mainEmail'
        },
        {
          name: '安全负责人',
          value: 'safety'
        },
        {
          name: '安全负责人<br>固定电话号码',
          value: 'safetyFix'
        },
        {
          name: '安全负责人<br>移动电话号码',
          value: 'safetyMobile'
        }
      ],
      safeListTwo: [
        {
          name: '安全负责人<br>电子邮箱',
          value: 'safetyEmail'
        },
        {
          name: '安全机构设置情况',
          value: 'safetyMechanism'
        },
        {
          name: '从业人员数量',
          value: 'employeesNumber'
        },
        {
          name: '特种作业人员数量',
          value: 'specialNumber'
        },
        {
          name: '专职安全生产管理人员数',
          value: 'managerNumber'
        },
        {
          name: '专职应急管理人员数',
          value: 'emergencyNumber'
        },
        {
          name: '注册安全工程师人员数',
          value: 'engineerNumber'
        },
        {
          name: '规模情况',
          value: 'scale'
        },
        {
          name: '企业规模',
          value: 'enterpriseScale'
        },
        {
          name: '安全监管监察机构',
          value: 'safetyRegulatorName'
        },
        {
          name: '生产/经营地址',
          value: 'businessAddress'
        }
      ],
      list: {},
      fileOne: {},
      fileTwo: {},
      fileTypeOne: {
        archivesType: '1',
        materialType: '001',
        method: 'POST',
        relationId: ''
      },
      fileTypeTwo: {
        archivesType: '2',
        materialType: '001',
        method: 'POST',
        relationId: ''
      }
    }
  },
  mounted() {
    ;(this.BASE_URL = BASE_URL), (this.BASE_IP = BASE_IP)
    this.getData()
  },
  methods: {
    async getData() {
      this.$set(this.fileTypeOne, 'relationId', this.customerId)
      this.$set(this.fileTypeTwo, 'relationId', this.customerId)
      const Res = await this.$get('/mobile/enterpriseInfo/safeProduct/' + this.customerId)
      const urlData = await this.$post('/system/archives/findByType', this.fileTypeOne)
      const urlDataTwo = await this.$post('/system/archives/findByType', this.fileTypeTwo)
      if (urlData && urlData.body) {
        this.fileOne = urlData.body
      }
      if (urlDataTwo && urlDataTwo.body) {
        this.fileTwo = urlDataTwo.body
      }
      if (Res && Res.body) {
        this.list = Res.body
      }
    },
    getValue(data) {
      let value = ''
      if (data === 'safetyMechanism') {
        if (this.list[data] === '0') {
          value = '有'
        } else if (this.list[data] === '1') {
          value = '无'
        }
      } else if (data === 'scale') {
        if (this.list[data] === '0') {
          value = '是'
        } else if (this.list[data] === '1') {
          value = '否'
        }
      } else if (data === 'enterpriseScale') {
        switch (this.list[data]) {
          case '1':
            value = '微型'
            break
          case '2':
            value = '小型'
            break
          case '3':
            value = '中型'
            break
          case '4':
            value = '大型'
            break
          default:
            break
        }
      } else {
        value = this.list[data]
      }
      return value
    },
    /**
     * 学习方式
     */
    goLearn(data) {
      if (
        [
          'png',
          'jpg',
          'jpeg',
          'gif',
          'PG',
          'PNG',
          'BMP',
          'JPG',
          'JPEG',
          'GIF',
        ].includes(data.fileType)
      ) {
        const url = [this.BASE_URL + '/system/archives/preview/thumb/' + data.archivesUrl]
        uni.previewImage({
          current: 0,
          urls: url,
          success: () => {},
          fail: () => {
            uni.showToast({
              title: '预览图片失败',
              icon: 'none',
            })
          },
        })
      } else {
        // 文档学习
        this.handleDownFile(data.archivesUrl, data.fileType)
      }
    },
    /**
     * 附件下载
     */
    handleDownFile(archivesUrl, fileType) {
      const downloadTask = uni.downloadFile({
        url: this.BASE_URL + '/system/archives/preview/files/' + archivesUrl,
        success: res => {
          if (res.statusCode === 200) {
            uni.showToast({ title: '下载成功', icon: 'none' })
          }
          // #ifndef H5
          uni.saveFile({
            tempFilePath: res.tempFilePath,
            success: function (red) {
              uni.openDocument({
                filePath: red.savedFilePath, // 支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
                fileType: fileType,
                showMenu: true, // 允许出现分享功能
                success: r => {
                },
                fail: error => {
                  uni.showToast({ title: '文件打开失败', icon: 'none' })
                }
              })
            }
          })
          // #endif
        },
        fail: err => {
          uni.showToast({ title: '下载失败', icon: 'none' })
        }
      })
      downloadTask.onProgressUpdate(res => {})
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
